package cn.jxau.fukin.util;

import static cn.jxau.fukin.util.ConstantUtil.deadlineMap;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DateTimeUtil {

	/**
	 * 判断一个月有多少天
	 * @param year
	 * @param month
	 * @return 该月的天数
	 */
	public static int getDaysByDate(int year, int month) {
		Calendar time = Calendar.getInstance();
		time.clear(); // 在使用set方法之前，必须先clear一下，否则很多信息会继承自系统当前时间
		time.set(Calendar.YEAR, year);
		time.set(Calendar.MONTH, month - 1);// Calendar对象默认一月为0
		int day = time.getActualMaximum(Calendar.DAY_OF_MONTH);// 本月份的天数
		return day;
	}

	/**
	 * 将字符串类型时间转化为Date类型
	 * @param str
	 * @return 转化好的Date类型的数据
	 */
	public static Date getParseDate(String str) {
		if (str == null)
			return null;
		Date date = null;
		try {
			date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		return date;
	}

	/**
	 * 根据起始时间和截止时间得到年数月数和日数
	 * @param start
	 * @param end
	 * @return map,根据key:year，month，day得到相应的值
	 */
	public static Map<String, Integer> getYearMonthAndDay(Date start, Date end) {
		if (start == null || end == null)
			return null;
		Calendar startTime = Calendar.getInstance();
		Calendar endTime = Calendar.getInstance();
		startTime.clear();
		endTime.clear();
		startTime.setTime(start);
		endTime.setTime(end);

		Map<String, Integer> map = new HashMap<String, Integer>();
		int resultYear = endTime.get(Calendar.YEAR) - startTime.get(Calendar.YEAR);
		int resultMonth = endTime.get(Calendar.MONTH) - startTime.get(Calendar.MONTH);
		int resultDay = endTime.get(Calendar.DAY_OF_MONTH) - startTime.get(Calendar.DAY_OF_MONTH);

		if (resultYear > 0 || resultMonth >= 0) {
			if (resultMonth < 0)// 未满一年
				resultYear--;
			map.put("year", resultYear);
		} else {
			map.put("year", 0);
		}
		if (resultMonth > 0 || resultDay >= 0) {
			if (resultDay < 0)// 未满一月
				resultMonth--;
			map.put("month", resultMonth);
		} else {
			map.put("month", resultMonth);
		}
		if (resultDay >= 0) {
			map.put("day", resultDay);
		} else {
			map.put("day", getDaysByDate(startTime.get(Calendar.YEAR), startTime.get(Calendar.MONTH) + 1) + resultDay);
		}
		return map;
	}

	/**
	 * 计算给定年数，月数，日数所得到的总天数
	 * @param year
	 * @param month
	 * @param day
	 * @return
	 */
	public static int getTotalDaysByDate(int year, int month, int day) {
		return day + month * 30 + year * 365;
	}

	/**
	 * 将时间相加
	 * @param curDate	当前时间
	 * @param time 期限
	 * @return	结束时间（Date）
	 */
	public static Date getEndTime(Date curDate, String time) {
		int duration = deadlineMap.get(time);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(curDate);
		int year = calendar.get(Calendar.YEAR);
		if (duration < 12) {
			int realTime = calendar.get(Calendar.MONTH) + duration;
			if (realTime < 12)
				calendar.set(Calendar.MONTH, realTime);
			else {
				realTime -= 12;
				calendar.set(Calendar.MONTH, realTime);
				calendar.set(Calendar.YEAR, year + 1);
			}

		} else if (duration >= 12) {
			calendar.set(Calendar.YEAR, year + duration / 12);
		}
		return calendar.getTime();
	}

	/**
	 * 这个方法可得到两个值，一个是包含当前时间的年月日，一个是当前时间的时分
	 * 
	 * @return list.get(0) 2017-09-18
	 * 		   list.get(1) 13:53
	 */
	public static List<String> getDayAndMinute() {
		ArrayList<String> list = new ArrayList<String>(2);
		StringBuilder sb = new StringBuilder(),
				sb2 = new StringBuilder();
		Calendar calendar = Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH)+1;
		int day = calendar.get(Calendar.DATE);
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		int minute = calendar.get(Calendar.MINUTE);
		sb.append(year).append("-").append(month).append("-").append(day);
		sb2.append(hour).append(":").append(minute);
		list.add(sb.toString());
		list.add(sb2.toString());
		return list;
	}
}
